package cn.est.serviceImpl;

import cn.est.dto.Page;
import cn.est.mapper.EvaluateDetailMapper;
import cn.est.pojo.Evaluate;
import cn.est.pojo.EvaluateDetail;
import cn.est.pojo.MalfunctionOptions;
import cn.est.service.EvaluateDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EvaluateDetailServiceImpl implements EvaluateDetailService {

    @Autowired
    private EvaluateDetailMapper evaluateDetailMapper;

    public EvaluateDetail getEvaluateDetailById(Long id) {
        return evaluateDetailMapper.getEvaluateDetailById(id);
    }

    public List<EvaluateDetail> getEvaluateDetailListByMap(Map<String, Object> param) {
        return evaluateDetailMapper.getEvaluateDetailListByMap(param);
    }

    public Integer getEvaluateDetailCountByMap(Map<String, Object> param) {
        return evaluateDetailMapper.getEvaluateDetailCountByMap(param);
    }

    public Integer qdtxAddEvaluateDetail(EvaluateDetail evaluateDetail) {
        evaluateDetail.setCreatdTime(new Date());
        return evaluateDetailMapper.insertEvaluateDetail(evaluateDetail);
    }

    public Integer qdtxModifyEvaluateDetail(EvaluateDetail evaluateDetail) {
        evaluateDetail.setUpdatedTime(new Date());
        return evaluateDetailMapper.updateEvaluateDetail(evaluateDetail);
    }

    public Integer qdtxDeleteEvaluateDetailById(Long id) {
        return evaluateDetailMapper.deleteEvaluateDetailById(id);
    }

    public Integer qdtxBatchDeleteEvaluateDetail(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return evaluateDetailMapper.batchDeleteEvaluateDetail(param);
    }

    public Page<EvaluateDetail> queryEvaluateDetailPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = evaluateDetailMapper.getEvaluateDetailCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<EvaluateDetail> evaluateDetailList = evaluateDetailMapper.getEvaluateDetailListByMap(param);
        page.setList(evaluateDetailList);
        return page;
    }

     @Override
    public List<EvaluateDetail> qdtxBatchAddOptions(List<MalfunctionOptions> malfunctionOptions, Evaluate e) {
        EvaluateDetail evaluateDetail;
        List<EvaluateDetail> list=new ArrayList<>();
        for (MalfunctionOptions options:
                malfunctionOptions) {
                evaluateDetail=new EvaluateDetail();
                evaluateDetail.setUpdatedUserId(e.getCreatedUserId());
                evaluateDetail.setCreatdTime(new Date());
                evaluateDetail.setEvaluateId(e.getId());
                evaluateDetail.setMalfId(options.getMalfId());
                evaluateDetail.setOptionId(options.getId());
                evaluateDetail.setOptionName(options.getOptionName());
                evaluateDetail.setIsDelete(0);
                evaluateDetailMapper.insertEvaluateDetail(evaluateDetail);
                list.add(evaluateDetail);
        }
        return list;
    }

    @Override
    public List<EvaluateDetail> getByEId(Long eid) {
        Map map=new HashMap();
        map.put("evaluateId",eid);
        return  evaluateDetailMapper.getEvaluateDetailListByMap(map);
    }

}
